package wyu.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dto.Result;
import com.entity.User;
import com.url.FilePathBase;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import wyu.mapper.UserMapper;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Override
    public User login(String name, String password) {
        User user = query().eq("uname", name).one();
        if (user==null){
            return null;
        }
        if (!password.equals(user.getUpwd())){
            return null;
        }
        return user;
    }

    @Override
    public User getUserByUname(String uName) {
        User user = query().eq("UNAME", uName).one();
        if (user == null){
            return null;
        }
        return user;
    }

    @Override
    public Result register(String uName, String password, String nickName, String motto, String email, String birthday, String intro, String[] area, MultipartFile headImg) {
        String path = FilePathBase.baseUrl + FilePathBase.userAvatar;
        File filePath = new File(path);
        String saveName;  /*文件保存名*/
        String separator = File.separator; //定义分隔符
        int i;  /*文件名 小数点位置*/
        String fileType;  /*文件类型 包括.*/


        User user = new User();
        user.setUname(uName);
        user.setUpwd(password);
        if (nickName==null){
            nickName="";
        }
        user.setNickName(nickName);
        if (motto==null){
            motto="";
        }
        user.setMotto(motto);
        if (email==null){
            email="";
        }
        user.setEmail(email);
        if (birthday==null){
            birthday="";
        }
        user.setBirthday(birthday);
        if (intro==null){
            intro="";
        }
        user.setIntro(intro);
        if (area==null){
            user.setAreaProvince("");
            user.setAreaCity("");
        }
        user.setAreaProvince(area[0]);
        user.setAreaCity(area[1]);
        //头像
        if (headImg==null){
            user.setHeadImg("");
        }
        user.setHeadImg(headImg.getOriginalFilename());

        try {
            if (headImg != null){
                // 如果上传了文件的话， 将头像以用户名（id） 保存     将文件保存到服务器
                if (headImg.getSize() != 0){
                    i = Objects.requireNonNull(headImg.getOriginalFilename()).lastIndexOf('.');
                    fileType = headImg.getOriginalFilename().substring(i);  /*包括.*/
                    fileType = fileType.toLowerCase(); //一律转小写
                    saveName = uName + fileType;

                    user.setHeadImg(saveName);
                    //headImg.transferTo(new File(filePath + separator + saveName));//文件名、目录名或卷标语法不正确
                    FileUtils.copyInputStreamToFile(headImg.getInputStream(),
                            new File(filePath + separator + saveName));
                }
            }
            //保存user
            boolean issave = save(user);
            if (!issave){
                return Result.fail(900,"注册失败");
            }
        }catch (IOException e){
            e.printStackTrace();
        }

        return Result.ok();
    }

    @Override
    public Result checkUserRight(User user) {
        if (user==null){
            System.out.println("user==null");
            return Result.fail(307,"请先登录！");
        }
        String role = user.getRole();
        if (role.equals("user")){
            return Result.fail(609,"权限不足！");
        }
        return Result.ok();
    }

}
